﻿using QG.JobBack.DAL;
using QH.JobBack.Model;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace QG.JobBack.BLL
{
    public class BaseService<T> where T : class, new()
    {
        private IRepository<T> db;

        public BaseService(IRepository<T> db)
        {
            this.db =db;
        }

        /// <summary>
        /// 通用新增方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Add(T entity)
        {
            return db.Add(entity);
        }

        /// <summary>
        /// 通用删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Delete(T entity)
        {
            return db.Delete(entity);

        }

        /// <summary>
        /// 通用批量删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Delete(List<T> entitys)
        {
            return db.Delete(entitys);
        }
        /// <summary>
        /// 通用批量删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Delete(params int[] ids)
        {
            return db.Delete(ids);
        }

        /// <summary>
        /// 通用修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Update(T entity)
        {
            return db.Update(entity);
        }

        /// <summary>
        /// 通用查询集合
        /// </summary>
        public virtual List<T> QueryList(Expression<Func<T, bool>> expression = null)
        {
            return db.QueryList(expression);
        }

        /// <summary>
        /// 查询返回对象
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual T Query(Expression<Func<T, bool>> expression = null)
        {
            return db.Query(expression);
        }

        /// <summary>
        /// 判断记录是否存在
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual bool Exists(Expression<Func<T, bool>> expression = null)
        {
            return db.Exists(expression);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <returns></returns>
        public virtual List<T> QueryPageList<S>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereExpression, Expression<Func<T, S>> orderExpression, bool isDsc = true)
        {
            return db.QueryPageList(pageIndex, pageSize, whereExpression, orderExpression, isDsc);
        }

        /// <summary>
        /// 返回总数
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public virtual int GetCount(Expression<Func<T, bool>> whereExpression, out int count)
        {
            return db.GetCount(whereExpression, out count);
        }


    }
}
